/* FinTech vs bank borrowers */
/* Clear all data from memory */
clear all

/* Load dataset for investment loans */
use "$output\matched_sample_nn_forinvestment_1.dta", clear

/* Keep observations within the study period (2014-2019) */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Keep observations within the event window (-12 to +24 months) */
keep if deltamonths >= -12
keep if deltamonths <= 24

/* Generate financial variables */
gen loan = stloan + ltloan + bm + bi + oc  /* Total loan amount */
gen leasing = bm + bi                      /* Leasing component */
gen lloan = log(loan)                      /* Log of total loan */

/* TREND RELATIVE Coefficient Plot */

/* Convert deltamonths into a non-negative categorical variable */
replace delta = deltamonths + 12

/* Define labels for deltamonths categories */
capture lab_delta drop 
forvalues n = 0(1)36 {
	local displayed_value = `n' - 12
	dis "`displayed_value'"
	label define lab_delta `n'  "`displayed_value'", add
}
label val delta lab_delta

/* Generate trend coefficient plots for log(loan) */
foreach var in lloan {
    /* Regression with fixed effects and clustering at firm level */
    qui eststo `var' : reghdfe `var' i1.p2p##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

	/* Figure 5a, left */
    /* Generate coefficient plot */
    coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
    ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
    omitted base keep(1.p2p#*) rename(1.p2p#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
    xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
    ylabel(-0.2(0.2)0.5, nogrid) xsize(6) ysize(4) ///
    plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")
    graph export "$figures/Figure_5al_trend_relative_`var'_coefplot_psm_forinvestment_1.pdf", replace
	
}

/* Repeat for non-investment loans borrowers */
use "$output\matched_sample_nn_forinvestment_0.dta", clear

/* Keep observations within the study period (2014-2019) */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Keep observations within the event window (-12 to +24 months) */
keep if deltamonths >= -12
keep if deltamonths <= 24

/* Generate financial variables */
gen loan = stloan + ltloan + bm + bi + oc  /* Total loan amount */
gen leasing = bm + bi                      /* Leasing component */
gen lloan = log(loan)                      /* Log of total loan */

/* TREND RELATIVE Coefficient Plot */

/* Convert deltamonths into a non-negative categorical variable */
replace delta = deltamonths + 12

/* Ensure sorted order */
sort siren delta
capture drop nb
bys siren delta: gen nb = _N
su nb, d
drop nb

/* Define labels for deltamonths categories */
capture lab_delta drop 
forvalues n = 0(1)36 {
	local displayed_value = `n' - 12
	dis "`displayed_value'"
	label define lab_delta `n'  "`displayed_value'", add
}
label val delta lab_delta

/* Generate trend coefficient plots for log(loan) */
foreach var in lloan {
    /* Regression with fixed effects and clustering at firm level */
    qui eststo `var' : reghdfe `var' i1.p2p##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

	/* Figure 5b, left */
    /* Generate coefficient plot */
    coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
    ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
    omitted base keep(1.p2p#*) rename(1.p2p#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
    xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
    ylabel(-0.4(0.2)0.4, nogrid) xsize(6) ysize(4) ///
    plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")
    graph export "$figures/Figure_5bl_trend_relative_`var'_coefplot_psm_forinvestment_0.pdf", replace
}


/**********************************************************************************
 * Load dataset for firms with no missing data *
 **********************************************************************************/
use "$output\matched_sample_no_missing.dta", clear

/* Keep observations within the study period (2014-2019) */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Keep observations within the event window (-12 to +24 months) */
keep if deltamonths >= -12
keep if deltamonths <= 24

/* Generate financial variables */
gen loan = stloan + ltloan + bm + bi + oc  /* Total loan amount */
gen leasing = bm + bi                      /* Leasing component */
gen lloan = log(loan)                      /* Log of total loan */

/* Ensure unique firm-month observations */
sort siren mdate
capture drop nb
bys siren mdate: gen nb = _N
su nb, d
drop nb

/* TREND RELATIVE Coefficient Plot */

/* Convert deltamonths into a non-negative categorical variable */
replace delta = deltamonths + 12

/* Ensure sorted order */
sort siren delta
capture drop nb
bys siren delta: gen nb = _N
su nb, d
drop nb

/* Define labels for deltamonths categories */
capture lab_delta drop 
forvalues n = 0(1)36 {
	local displayed_value = `n' - 12
	dis "`displayed_value'"
	label define lab_delta `n'  "`displayed_value'", add
}
label val delta lab_delta

/* Generate trend coefficient plots for firms with no missing data */
foreach var in lloan {
	
	*Figure D1A
    /* Regression with fixed effects and clustering at firm level */
    qui eststo `var' : reghdfe `var' i1.p2p##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

    /* Generate coefficient plot */
    coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
    ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
    omitted base keep(1.p2p#*) rename(1.p2p#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
    xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
    ylabel(-0.2(0.1)0.3, nogrid) xsize(6) ysize(4) ///
    plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")
    graph export "$figures/Figure_D1a_trend_relative_`var'_coefplot_psm_nomissing_0.pdf", replace
}

